// Gaia Ajax Widgets Copyright (C) 2007  Frost Innovation AS. details at http://ajaxwidgets.com/

/* 
 * Gaia Ajax Widgets, an Ajax Widget Library
 * Copyright (C) 2007  Frost Innovation AS
 * All rights reserved.
 * This program is distributed under either GPL version 2 
 * as published by the Free Software Foundation or the
 * Gaia Commercial License version 1 as published by
 * Frost Innovation AS
 * read the details at http://ajaxwidgets.com/
 */




/* ---------------------------------------------------------------------------
   Class basically wrapping the ASP.Accordion WebControl class
   --------------------------------------------------------------------------- */
Gaia.MultiAccordion = function(element, options){
  this.initialize(element, options);
}

// Inheriting from WebControl
Object.extend(Gaia.MultiAccordion.prototype, Gaia.WebControl.prototype);

// Inheriting from Container
Object.extend(Gaia.MultiAccordion.prototype, Gaia.Container.prototype);

Object.extend(Gaia.MultiAccordion.prototype, {

  // "Constructor"
  initialize: function(element, options){
    // Calling base class constructor
    this.baseInitializeWebControl(element, options);

    this.setExpanded (this.options.is_expanded);
    this.getContentWrapper().style.height = this.options.height+'px';
    this._onClicked = this.clicked.bind(this);
    Element.observe(this.getHeader(), 'click', this._onClicked);

    // Checking to see if there's no css class given and if not creating some "default" colors...
    if( !this.options.className || this.options.className == '' ){
      this.getHeader().style.backgroundColor = 'Red';
    }
  },

  clicked: function(){
    this.setExpanded(!this.options.is_expanded);
  },
  
  setExpanded: function(value){
    if( value )
      Element.show(this.getContentWrapper());
    else
      Element.hide(this.getContentWrapper());
    this.options.is_expanded = value;
  },

  getContentWrapper: function(){
    if( !this.contentDiv ){
      this.contentDiv = $(this.element.id.substr(0,this.element.id.length-4)+'_DIV');
    }
    return this.contentDiv;
  },

  getHeader: function(){
    if( !this.headerDiv ){
      this.headerDiv = $(this.element.id.substr(0,this.element.id.length-4)+'_DIV_CAPTION');
    }
    return this.headerDiv;
  },

  destroy: function(){

    // Destroying all CHILDREN controls
    this.destroyChildrenControls();

    // Stopping observers...
    Element.stopObserving(this.headerDiv, 'click', this._onClicked);

    // Dispatching to container DTOR
    this.destroyContainer(this.element.id);

    // Calling Object.destroy implementation...
    this._destroyImpl();
  },
  
  _getElementPostValue: function(){
    return '';
  }
});

Gaia.Accordion.browserFinishedLoading = true;
